package com.link.base.base.user.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.user.model.UserChange;
import com.link.base.base.user.service.UserService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.interceptor.SecurityInterceptor;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.AppConstants;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户工具类
 *
 * @author ouyangzhenqiang
 * @date 2018/11/1
 */
@Controller
@RequestMapping("/link/userUtil")
public class UserUtilController {

    @Resource
    private UserService userService;

    /**
     * 移动端修改密码
     *
     * @author
     * @date 2018/11/1
     */
    @RequestMapping(value = "/changePassword", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> changePassword(@JsonParam UserChange userChange, HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (userChange != null) {
                CoreUser user = SecurityInterceptor.tempUser.get();
                LCLogger.info().withMessageKey("changePassword")
                        .withMessage(AppConstants.LOG_DIVISION_LINE + "modify password start！").flush();
                userService.modifyPassword(user, userChange.getOldPassword(), userChange.getNewPassword(), userChange.getConfirmPassword());
                LCLogger.info().withMessageKey("changePassword")
                        .withMessage(AppConstants.LOG_DIVISION_LINE + "modify password end！").flush();
            } else {
                LCLogger.info().withMessageKey("changePassword")
                        .withMessage(AppConstants.LOG_DIVISION_LINE + "the info of password is null！").flush();
                throw new ServiceException("USER-045");
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.error().withMessageKey("changePassword")
                    .withMessage(AppConstants.LOG_DIVISION_LINE + e.getMessage()).flush();
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 修改密码
     *
     * @author
     * @date 2018/11/1
     */
    @RequestMapping(value = "/changeEmployeePassword", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> changeEmployeePassword(UserChange userChange, HttpSession session,
                                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (userChange != null) {
                LCLogger.info().withMessageKey("changeEmployeePassword")
                        .withMessage(AppConstants.LOG_DIVISION_LINE + "modify password start！").flush();
                userService.modifyEmpLoyeePassword(userChange.getId(), userChange.getNewPassword(), userChange.getConfirmPassword());
                LCLogger.info().withMessageKey("changeEmployeePassword")
                        .withMessage(AppConstants.LOG_DIVISION_LINE + "modify password end！").flush();
            } else {
                LCLogger.info().withMessageKey("changeEmployeePassword")
                        .withMessage(AppConstants.LOG_DIVISION_LINE + "the info of password is null！").flush();
                throw new ServiceException("USER-045");
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.error().withMessageKey("changeEmployeePassword")
                    .withMessage(AppConstants.LOG_DIVISION_LINE + e.getMessage()).flush();
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
